//
//  PlaceOrderViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/21.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "PlaceOrderViewController.h"
#import "SureOrderViewController.h"
@interface PlaceOrderViewController ()
{
    NSDictionary *dicData;
    
}
@end

@implementation PlaceOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addLeftBackBtn:nil];
    self.title=@"下单";
    _layout.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:UIColorFromRGB(0xe5e5e5)]; //底部边界线设置可以缩进
    _layout.bottomBorderLine.headIndent = 16;
    
    _layout2.bottomBorderLine = [[MyBorderLineDraw alloc] initWithColor:UIColorFromRGB(0xe5e5e5)]; //底部边界线设置可以缩进
    _layout2.bottomBorderLine.headIndent = 16;
    _chaXun.layer.cornerRadius=20;
    
    __weak typeof(self)weakSelf=self;
    
    RACSignal *yibaoSignal=[_yiBaoNumber.rac_textSignal map:^id(NSString * value) {
        if (weakSelf.mobileNumber.text.length==11) {
            _yiBaoNumber.text=@"";
            [self.view makeToast:@"医保号和手机号只能选择一个"];

        }
        if (_yiBaoNumber.text.length==12) {
            _mobileNumber.text=@"";
            
        }
        return @([self isValidYibao:value]);
      
    }];
    
    RACSignal *mobileSignal=[_mobileNumber.rac_textSignal map:^id(NSString * value) {
        if (weakSelf.yiBaoNumber.text.length==12) {
            _mobileNumber.text=@"";
            [self.view makeToast:@"医保号和手机号只能选择一个"];
        }
        if (_mobileNumber.text.length==11) {
            _yiBaoNumber.text=@"";

        }
        return @([self isValidateMobile:value]);
        
    }];
    
    
    RACSignal *creatSignal=[RACSignal combineLatest:@[yibaoSignal,mobileSignal] reduce:^id(NSNumber*yibao, NSNumber *mobile){
    
        return @([yibao boolValue]||[mobile boolValue]);

    }];
    [creatSignal subscribeNext:^(NSNumber*signupActiv) {
        _chaXun.enabled=[signupActiv boolValue];        
        
        _chaXun.backgroundColor=[signupActiv boolValue]?UIColorFromRGB(0x00ccc2):UIColorFromRGB(0xe5e5e5);
        
    }];
    
    
    [[[[_chaXun rac_signalForControlEvents:UIControlEventTouchUpInside]doNext:^(id x) {
        _chaXun.enabled =NO;

    }]
     flattenMap:^id(id x){
         
         return[self signInSignal];
     }]
    
     subscribeNext:^(NSNumber*signedIn){
         BOOL success =[signedIn boolValue];
         _chaXun.enabled =YES;

         if(success){

             ALLOC(SureOrderViewController, view);
             view.dic=dicData;
             
             [self pushNextViewController:view];
         }
         
      

        
    }];
    
    
    
    // Do any additional setup after loading the view from its nib.
}


- (RACSignal *)signInSignal {
    return [RACSignal createSignal:^RACDisposable *(id subscriber){
        
        [self getUserInfoRequestDataWithResponseBlock:^(BOOL success) {
            
            [subscriber sendNext:@(success)];
            [subscriber sendCompleted];
        }];
        
        return nil;
    }];
}
-(void)getUserInfoRequestDataWithResponseBlock:(surebtnResponse)completeBlock
{
    
    
    [self addProgressHUD];
    
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    [dic setObject:self.yiBaoNumber.text forKey:@"medicareCard"];
    [dic setObject:self.mobileNumber.text forKey:@"mobile"];
    [dic setObject:[self getArtisanId] forKey:@"artisanId"];

    
    NSLog(@"=======%@",dic);

    [[WSNetworkApi shareManager]postWithUrl:@"/user/getUserInfo" parameters:dic option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            dicData=responseObject[@"user"];
            
            completeBlock(YES);
            
            
        }
        else{
            completeBlock(NO);
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        
    } failur:^(NSError *error) {
        [self removeStatuslable];
        
    }];
    
}


- (BOOL)isValidYibao:(NSString *)username {
    return username.length ==12;
}
-(BOOL) isValidateMobile:(NSString *)mobile//手机号码验证
{
    //手机号以13， 14，15，18，17开头，八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
